﻿using System;
using System.Reflection;
using Abp.Extensions;

namespace DHJ.Cost.Tasks
{
    /// <summary>
    /// Used to identify a batch. Contains batchId and quantity.
    /// </summary>
    [Serializable]
    public class BatchIdentifier : IBatchIdentifier
    {
        public const char Divider = '@';

        /// <summary>
        /// Id of the batch. Usually a barcode.
        /// </summary>
        public string BatchId { get; }

        /// <summary>
        /// Current quantity.
        /// </summary>
        public int Quantity { get; }

        private BatchIdentifier()
        {

        }

        public BatchIdentifier(string batchId, int quantity)
        {
            if (batchId.IsNullOrWhiteSpace() || quantity<=0)
                throw new ArgumentNullException();
            BatchId = batchId;
            Quantity = quantity;
        }

        /// <summary>
        /// Parses given string and creates a new <see cref="BatchIdentifier"/> object.
        /// </summary>
        /// <param name="batchIdentifierString">
        /// Should be following format: "batchId_quantity". Ex: "123456-001_10".
        /// </param>
        public static BatchIdentifier Parse(string batchIdentifierString)
        {
            if (batchIdentifierString.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(batchIdentifierString), "batchId_quantity can not be null or empty!");
            }

            var result = batchIdentifierString.Split(Divider);
            if (result.Length == 2)
            {
                return new BatchIdentifier(result[0], result[1].To<int>());
            }
            throw new ArgumentException("batchId_quantity is not properly formatted", nameof(batchIdentifierString));
        }

        /// <summary>
        /// Creates a string represents this <see cref="BatchIdentifier"/> instance.
        /// Formatted as follows: "batchId_quantity". Ex: "123456-001_10".
        /// </summary>
        /// <returns>Return string can ben used in <see cref="Parse"/> method to re-create identical <see cref="BatchIdentifier"/> object.</returns>
        public string ToBatchIdentifierString()
        {
            return BatchId + Divider + Quantity;
        }

        public override string ToString()
        {
            return ToBatchIdentifierString();
        }

        public override bool Equals(object obj)
        {

            if (!(obj is BatchIdentifier))
            {
                return false;
            }

            //Same instances must be considered as equal
            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            //Transient objects are not considered as equal
            var other = (BatchIdentifier)obj;

            //Must have a IS-A relation of types or must be same type
            var typeOfThis = GetType();
            var typeOfOther = other.GetType();
            if (!typeOfThis.GetTypeInfo().IsAssignableFrom(typeOfOther) && !typeOfOther.GetTypeInfo().IsAssignableFrom(typeOfThis))
            {
                return false;
            }

            return BatchId == other.BatchId && Quantity == other.Quantity;
        }

        public override int GetHashCode()
        {
            return BatchId.GetHashCode() * Quantity;
        }

        public static bool operator ==(BatchIdentifier left, BatchIdentifier right)
        {
            return Equals(left, null) ? Equals(right, null) : Equals(left, right);
        }

        public static bool operator !=(BatchIdentifier left, BatchIdentifier right)
        {
            return !(left == right);
        }
    }
}